package com.metadata_system.api_server.system_server;


import com.alibaba.fastjson.JSON;
import com.metadata_system.api.annotations.CheckTokenUser;
import com.metadata_system.common.dto.ApiResult;
import com.metadata_system.common.http.request_dto.LoginUserRequest;
import com.metadata_system.system.request.ProjectLogListParams;
import com.metadata_system.system.service.ProjectLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController("systemServerProjectLogController")
@RequestMapping(value = "/api/system-server/v001/ProjectLog/")
@Slf4j
public class ProjectLogController {
    @Autowired
    ProjectLogService projectLogService;
    @Autowired
    LoginUserRequest loginUserRequest;


    @RequestMapping(value = "getList", method = {RequestMethod.POST, RequestMethod.GET})
    @CheckTokenUser
    public ApiResult getList(
            @RequestParam(required = false, defaultValue = "apply") String showType,
            @RequestParam(required = false) Integer projectId,
            @RequestParam(required = false) String projectNodeNames,
            @RequestParam(required = false) Integer metadataId,
            @RequestParam(required = false) String readStatus,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "20") Integer pageSize,
            @RequestParam(required = false, defaultValue = "createdTime") String sortProp,
            @RequestParam(required = false, defaultValue = "DESC") String sortOrder
    ) {
        return projectLogService.getList(ProjectLogListParams.builder()
                .userId(loginUserRequest.getTokenUser().decodeID())
                .showType(showType)
                .projectId(projectId)
                .projectNodeNames(projectNodeNames)
                .metadataId(metadataId)
                .status(status)
                .readStatus(readStatus)
                .type(type)
                .page(page)
                .pageSize(pageSize)
                .sortProp(sortProp)
                .sortOrder(sortOrder)
                .build());
    }



    @RequestMapping(value = "cancel", method = {RequestMethod.POST, RequestMethod.GET})
    @CheckTokenUser
    @Transactional
    public ApiResult cancel(
            @RequestParam Integer id
    ) {
        return projectLogService.cancel(id, loginUserRequest.getTokenUser().decodeID());
    }

    @RequestMapping(value = "delete", method = {RequestMethod.POST, RequestMethod.GET})
    @CheckTokenUser
    @Transactional
    public ApiResult delete(
            @RequestParam Integer id
    ) {
        return projectLogService.delete(id, loginUserRequest.getTokenUser().decodeID());
    }

    @RequestMapping(value = "submitImport", method = {RequestMethod.POST, RequestMethod.GET})
    @CheckTokenUser
    @Transactional
    public ApiResult submitImport(
            @RequestParam String jsonData,
            @RequestParam Integer projectId
    ) {
        return projectLogService.submitImport(jsonData, loginUserRequest.getTokenUser().decodeID(), projectId);
    }

    @RequestMapping(value = "submit", method = {RequestMethod.POST, RequestMethod.GET})
    @CheckTokenUser
    @Transactional
    public ApiResult submit(
            @RequestParam String jsonData,
            @RequestParam Integer projectId,
            @RequestParam Integer metadataNodeId
    ) {
        return projectLogService.submit(projectId, metadataNodeId, loginUserRequest.getTokenUser().decodeID(), jsonData);
    }

    @RequestMapping(value = "reSubmitImport", method = {RequestMethod.POST, RequestMethod.GET})
    @CheckTokenUser
    @Transactional
    public ApiResult reSubmitImport(
            @RequestParam String jsonData,
            @RequestParam Integer id
    ) {
        return projectLogService.reSubmitImport(jsonData, id, loginUserRequest.getTokenUser().decodeID());
    }

    @RequestMapping(value = "agree", method = {RequestMethod.POST, RequestMethod.GET})
    @CheckTokenUser
    @Transactional
    public ApiResult agree(
            @RequestParam Integer id
    ) {
        return projectLogService.agree(id, loginUserRequest.getTokenUser().decodeID());
    }

    @RequestMapping(value = "refuse", method = {RequestMethod.POST, RequestMethod.GET})
    @CheckTokenUser
    @Transactional
    public ApiResult refuse(
            @RequestParam Integer id,
            @RequestParam String refuseReason
    ) {
        return projectLogService.refuse(id, loginUserRequest.getTokenUser().decodeID(), refuseReason);
    }

    @RequestMapping(value = "lock", method = {RequestMethod.POST, RequestMethod.GET})
    @CheckTokenUser
    @Transactional
    public ApiResult lock(
            @RequestParam Integer id,
            @RequestParam(required = false, defaultValue = "1") Integer lockStatus
    ) {
        return projectLogService.lockItem(id, loginUserRequest.getTokenUser().decodeID(), lockStatus);
    }

    @RequestMapping(value = "read", method = {RequestMethod.POST, RequestMethod.GET})
    @CheckTokenUser
    @Transactional
    public ApiResult read(
            @RequestParam Integer id
    ) {
        return projectLogService.readItem(id, loginUserRequest.getTokenUser().decodeID());
    }

    @RequestMapping(value = "handlerRead", method = {RequestMethod.POST, RequestMethod.GET})
    @CheckTokenUser
    @Transactional
    public ApiResult handlerRead(
            @RequestParam Integer id
    ) {
        return projectLogService.handlerRead(id, loginUserRequest.getTokenUser().decodeID());
    }

    @RequestMapping(value = "getDataDetail", method = {RequestMethod.POST, RequestMethod.GET})
    @CheckTokenUser
    @Transactional
    public ApiResult getDataDetail(
            @RequestParam Integer id
    ) {
        return projectLogService.getDataDetail(id, loginUserRequest.getTokenUser().decodeID());
    }


    @RequestMapping(value = "submitDelete", method = {RequestMethod.POST, RequestMethod.GET})
    @CheckTokenUser
    @Transactional
    public ApiResult submitDelete(
            @RequestParam Integer metadataNodeId,
            @RequestParam Integer projectId,
            @RequestParam String ids
    ) {
        List<Integer> idList=new ArrayList<>();
        try {
            idList = JSON.parseArray(ids, Integer.class);
        }catch (Exception e){
            return ApiResult.error("ids格式错误");
        }
        return projectLogService.submitDelete(idList,projectId, metadataNodeId, loginUserRequest.getTokenUser().decodeID());
    }


    @RequestMapping(value = "rollBack", method = {RequestMethod.POST, RequestMethod.GET})
    @CheckTokenUser
    @Transactional
    public ApiResult rollBack(
            @RequestParam Integer id
    ) {
        return projectLogService.rollBack(id, loginUserRequest.getTokenUser().decodeID());
    }


}
